package com.blue.controller.system;

import com.blue.common.entity.GenerateSet;
import com.blue.common.entity.ResponseResult;
import com.blue.common.utils.BusinessUtils;
import com.blue.service.system.IGenerateService;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.servlet.http.HttpServletResponse;
import org.apache.commons.io.IOUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;
import java.util.List;
import java.util.Map;

/**
 * @author nanjie.chen
 * Created on 2023/9/22
 */
@RestController
@RequestMapping("/system/generate")
@Tag(name = "系统代码生成")
public class GenerateController {
    @Autowired
    private IGenerateService generateService;

    @GetMapping("/pageList")
    public ResponseResult<?> pageList(String tableName,
                                      @RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize,
                                      @RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum) {
        List<Map<String, String>> list = generateService.pageList(tableName);
        return ResponseResult.okResult(BusinessUtils.getPage(pageNum, pageSize, list));
    }


    @PostMapping("/generateCode")
    public void generateCode(@RequestBody GenerateSet set, HttpServletResponse response) throws IOException {
        byte[] data = generateService.generateCode(set);
        response.setContentType("application/octet-stream; charset=UTF-8");
        IOUtils.write(data, response.getOutputStream());
    }
}
